package cz.cuni.mff.ufal.volk;

import java.util.List;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import cz.cuni.mff.ufal.volk.il.Node;
import cz.cuni.mff.ufal.volk.message.ChoreographyBuilder;
import cz.cuni.mff.ufal.volk.nabaztag.Ear;
import cz.cuni.mff.ufal.volk.nabaztag.Led;
import cz.cuni.mff.ufal.volk.services.Services;
import cz.cuni.mff.ufal.volk.services.TextToSpeech;

public class NabaztagSimpleInterpreter implements Listener<Node> {

	private static final Logger log = Logger.getLogger(NabaztagSimpleInterpreter.class);
	private final ResourceBundle messages =
		ResourceBundle.getBundle("volk/resources/messages", ApplicationSettings.getCurrent().getLocale());

	public NabaztagSimpleInterpreter(NabaztagController controller) {
		controller.getClass();
		this.controller = controller;
	}

	private final NabaztagController controller;

	public void process(Node node) {
		log.info("trying to interprete: " + node.toString());

		if ("true".equals(node.getAttributes().get("dna")))
			return;

		String mood = node.getAttributes().get("mood");
		log.trace("mood=" + mood);
		if (!"imp".equals(mood)) {
			// as far, we can work with imperative verbs only
			String message = messages.getString("nsi.imperative.only");
			TextToSpeech tts = Services.get(Services.TEXT_TO_SPEECH, TextToSpeech.class);
			Speech soundMessage = tts.process(new Text(ApplicationSettings.getCurrent().getLocale().getLanguage(), message), null);
			for (NabaztagProxy nab : controller.getRegisteredBunnies()) {
				nab.playSound(soundMessage);
			}
			return;
		}

		ChoreographyBuilder cb = new ChoreographyBuilder();
		int red = 100, green = 100, blue = 0;

		if (node.getWord().equals("switch_on")) {
			List<Node> what = node.getChildren().get("dir_obj");

			for (Node led : what) {
				if (!led.getWord().equals("led"))
					continue;

				List<Node> colors = node.getChildren().get("how");
				if (colors != null) {
					for (Node color : colors) {
						if (color.getWord().equals("red")) {
							red = 255;
							green = 0;
							blue = 0;
						}
						if (color.getWord().equals("green")) {
							red = 0;
							green = 255;
							blue = 0;
						}
						if (color.getWord().equals("blue")) {
							red = 0;
							green = 0;
							blue = 255;
						}
						if (color.getWord().equals("yellow")) {
							red = 255;
							green = 255;
							blue = 0;
						}
					}
				}

				List<Node> adjs = led.getChildren().get("adj");
				if (adjs != null) {
					for (Node adj : adjs) {
						if (adj.getWord().equals("left"))
							cb.appendLed(0, Led.LEFT, red, green, blue);
						if (adj.getWord().equals("right"))
							cb.appendLed(0, Led.RIGHT, red, green, blue);
						if (adj.getWord().equals("top"))
							cb.appendLed(0, Led.NOSE, red, green, blue);
						if (adj.getWord().equals("bottom"))
							cb.appendLed(0, Led.CENTER, red, green, blue);
					}
				}
			}
		}

		if (node.getWord().equals("wave")) {
			List<Node> with = node.getChildren().get("with");
			if (with != null) {
				for (Node obj : with) {
					if (!obj.getWord().equals("ear"))
						continue;

					List<Node> adjs = obj.getChildren().get("adj");
					if (adjs != null) {
						for (Node adj : adjs) {
							if (adj.getWord().equals("left"))
								cb.appendEarStep(Ear.LEFT, 17);
							if (adj.getWord().equals("right"))
								cb.appendEarStep(Ear.RIGHT, 17);
						}
					}
				}
			}
		}

		cb.appendRandomMidi(10);
		for (NabaztagProxy nab : controller.getRegisteredBunnies()) {
			nab.playChoreography(cb.getBytes());
		}

	}

}
